<template>
<h2 style="text-align: center">用户登录</h2>
<table>
    <tr>
        <td>用户名</td>
        <td><input type="text" v-model="info.name"></td>
    </tr>
    <tr>
        <td>密码</td>
        <td><input type="password" v-model="info.pwd"></td>
    </tr>
    <tr>
        <td colspan="2" style="text-align: center">
            <button @click="login">登录</button>
        </td>
    </tr>
    
</table>
</template>
<script setup lang="ts">
import {ref} from 'vue'
import axios from 'axios'
import {useRouter} from 'vue-router'

const router = useRouter()



const info = ref({
    name:'',
    pwd:''
})

const login = ()=>{
    axios.get('http://localhost:58004//api/Users/UserLogin',{
        params:{
            name:info.value.name,
            pwd:info.value.pwd
        }
    })
    .then(res=>{
        console.log(res.data)
        
        if(res.data.Rm_Id==0){
            alert(res.data.RM_Name)
            SearchUserInfo(res.data.RM_Data[0].U_Id)
            
        }else if(res.data.Rm_Id==1)
        {
            alert(res.data.RM_Name)
        }
        else if(res.data.Rm_Id==2)
        {
            alert(res.data.RM_Name)
        }
        else{
            alert("登录失败")
        }
    })
    .catch(err=>{
        console.log(err)
    })
}


const SearchUserInfo=(U_Id:string) =>{
    alert(U_Id)
    axios.get('http://localhost:58004/api/Users/SelUserInfor',{
        params:{
            U_Id:U_Id
        }
    })
    .then(res=>{
        console.log(res.data)
        
        if(res.data>0){
            alert("即将跳转到主页面")

        }
        else
        {
            router.push({
                path: "/UserRegister",
                query: {
                    U_Id: U_Id,
                }
            })
        }
    })
    .catch(err=>{
        console.log(err)
    })
}

</script>
